<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习8</title>
</head>

<body>

</body>
<script>
    //1.查看字符串中是否包含数字
    function hasNumber(str) {
        return /\d/.test(str);
    }

    console.log(hasNumber("abc123"));
    console.log(hasNumber("abcdef"));

    //2.验证中文名，且2到5位
    function validChineseName(name) {
        return /^[\u4e00-\u9fa5]{2,5}$/.test(name);
    }

    console.log(validChineseName("张三"));
    console.log(validChineseName("张a"));

    //3.将以下字符串中的数字全部筛选出来  中国移动:10086,中国联通:10010,中国电信:10000  match()
    const str = "中国移动:10086,中国联通:10010,中国电信:10000";
    const numbers = str.match(/\d+/g);

    console.log(numbers);

    //4.提取年月日的日期中的年份 2022-10-20
    //正则表达式中()进行分组使用，获取分组匹配到的结果用RegExp.$1  RegExp.$2
    let dateStr = "2022-10-20";
    let reg = /^(\d{4})-(\d{2})-(\d{2})$/
    if (reg.test(dateStr)) {
        console.log(RegExp.$1);
    }

    //5.将邮箱的各部分进行提取  123456@qq.com
    let email = "123456@qq.com";
    let reg1 = /^(\w+)@([a-zA-Z0-9]+)\.([a-zA-Z]{2,})$/;
    if (reg1.test(email)) {
        console.log(RegExp.$1);
        console.log(RegExp.$2);
        console.log(RegExp.$3);
    }

    //6.'abc   123   aaa   bbb' 将此字符串中的空格替换成AAA  replace()
    let str1 = 'abc   123   aaa   bbb';
    let newStr = str1.replace(/\s+/g, 'AAA');
    console.log(newStr);

    //7.切割字符串  abc#123@bbb  split()
    let str2 = 'abc#123@bbb';
    let arr = str2.split(/[#@]/);
    console.log(arr);
</script>

</html>